<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class Course extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function dglist(){
        if(request()->isAjax()){
            $where = [];
            if(session('groupid') == 2){
                $where['l_piano'] = ['like','%' . session('piano') . '%'];
            }
            $keyword = input('keyword');
            $user_school = input('user_school');
            $utype = input('utype');
            if(isset($keyword) && $keyword != ''){
                $where['l_title|user_username'] = ['like', '%' . $keyword . '%'];
            }
            //琴行
            if(isset($user_school) && $user_school != "")
            {
                $where['l_piano'] = ['like','%'.$user_school.'%'];
            }
            if(isset($utype) && $utype != "")
            {
                $where['l_type'] = $utype;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'l.*,user_username,user_phone';
            $count = Db::table('up_list')->alias('l')
                ->join('up_u_user u','l.l_student=u.user_id','LEFT')
                ->where($where)
                ->field($field)
                ->count();// 获取总条数
            $lists = Db::table('up_list')->alias('l')
                ->join('up_u_user u','l.l_student=u.user_id','LEFT')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('l_time desc')
                ->field($field)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $panio = Db::table('up_piano')
            ->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign([
            'panio' => $panio,
        ]);
        return $this->fetch('course/index');
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $user_id = input('id');
        $res = Db::table('up_list')->where('l_id', $user_id)->setField('l_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $user_id = input('id');
        $res = Db::table('up_list')->where('l_id', $user_id)->setField('l_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function deluser(){
        $user_id = input('id');
        $res = Db::table('up_list')->where('l_id', $user_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 添加
     * @return mixed
     */
    public function addList(){
        if(Request::instance()->isPost()){
            $l_title = input('l_title');
            $l_status = input('l_status');
            $l_piano = input('l_piano');
            $l_student = input('l_student');
            $l_content = input('l_content');
            $insert = [
                'l_title' => $l_title,
                'l_status' => $l_status,
                'l_piano' => $l_piano,
                'l_student' => $l_student,
                'l_content' => $l_content,
                'l_type' => 1,
                'l_admin' => session('username'),
                'l_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_list')->insertGetId($insert);
            if($res){
                try{
                    $con = '您有新的课程大纲：' . $l_title;
                    $nid = allInNotice('您有新的课程大纲',$con,5,$l_student,$res);
                    $this->Jpush(1, $l_student, '您有新的课程大纲', ['type'=>5,'nid'=>$nid]);
                }catch(\Exception $e){

                }
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $panio = Db::table('up_piano')
            ->order('pa_id desc')
            ->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign('panio', $panio);
        return $this->fetch('course/addlist');
    }
    /**
     * 添加（琴行类型）
     * @return mixed
     */
    public function addListPiano(){
        if(Request::instance()->isPost()){
            $l_title = input('l_title');
            $l_status = input('l_status');
            $l_piano = input('l_piano');
            $l_content = input('l_content');
            $insert = [
                'l_title' => $l_title,
                'l_status' => $l_status,
                'l_piano' => $l_piano,
                'l_content' => $l_content,
                'l_type' => 2,
                'l_admin' => session('username'),
                'l_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_list')->insertGetId($insert);
            if($res){
                $ids = Db::table('up_u_user')
                    ->where('user_status',1)
                    ->where('user_type',1)
                    ->where('user_piano','in',$l_piano)
                    ->column('user_id');
                if($ids){
                    foreach ($ids as $k => $v) {
                        try{
                            $con = '您有新的课程大纲：' . $l_title;
                            $nid = allInNotice('您有新的课程大纲',$con,5,$v,$res);
                            $this->Jpush(1, $v, '您有新的课程大纲', ['type'=>5,'nid'=>$nid]);
                        }catch(\Exception $e){

                        }
                    }
                }
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $panio = Db::table('up_piano')
            ->order('pa_id desc')
            ->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign('panio', $panio);
        return $this->fetch('course/addlistpiano');
    }
    /**
     * 根据琴行获取对应的学生
     * @return mixed
     */
    public function getUserByPiano(){
        if(Request::instance()->isPost()){
            $students = Db::table('up_u_user')
                ->where('user_status',1)
                ->where('user_type',1)
                ->where('user_piano',$_POST['pid'])
                ->field(['user_id'=>'id','concat(user_username,"(",user_phone,")")' => 'name'])
                ->select();
            return json(['code' => 200, 'msg' => $students]);
        }
    }
    /**
     * 编辑
     * @return mixed
     */
    public function memberEdit(){
        $l_id = input('l_id');
        $info = Db::table('up_list')
            ->where('l_id', $l_id)
            ->find();
        if(Request::instance()->isPost()){
            $l_title = input('l_title');
            $l_status = input('l_status');
            $l_piano = input('l_piano');
            $l_content = input('l_content');
            $update = [
                'l_title' => $l_title,
                'l_status' => $l_status,
                'l_piano' => $l_piano,
                'l_content' => $l_content,
                'l_admin' => session('username'),
                'l_time' => date('Y-m-d H:i:s')
            ];
            if($info['l_type'] == 2){
                $l_student = input('l_student');
                $update['l_student'] = $l_student;
            }
            $res = Db::table('up_list')->where('l_id',$l_id)->update($update);
            if($res){
                if($info['l_type'] == 1){
                    try{
                        $con = '您的课程大纲更新了：' . $l_title;
                        $nid = allInNotice('您的课程大纲更新了',$con,5,$info['l_student'],$l_id);
                        $this->Jpush(1, $info['l_student'], '您的课程大纲更新了', ['type'=>5,'nid'=>$nid]);
                    }catch(\Exception $e){

                    }
                }else{
                    $ids = Db::table('up_u_user')
                        ->where('user_status',1)
                        ->where('user_type',1)
                        ->where('user_piano','in',$l_piano)
                        ->column('user_id');
                    if($ids){
                        foreach ($ids as $k => $v) {
                            try{
                                $con = '您的课程大纲更新了：' . $l_title;
                                $nid = allInNotice('您的课程大纲更新了',$con,5,$v,$l_id);
                                $this->Jpush(1, $v, '您的课程大纲更新了', ['type'=>5,'nid'=>$nid]);
                            }catch(\Exception $e){

                            }
                        }
                    }
                }
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $panio = Db::table('up_piano')->select();
        $students = Db::table('up_u_user')
            ->where('user_status',1)
            ->where('user_type',1)
            ->where('user_piano',$info['l_piano'])
            ->field(['user_id'=>'id','concat(user_username,"(",user_phone,")")' => 'name'])
            ->select();
        $info['l_piano'] = explode(',',$info['l_piano']);
        $this->assign('info', $info);
        $this->assign('l_id', $l_id);
        $this->assign('panio', $panio);
        $this->assign('students', $students);
        $type = 0;
        if(isset($_REQUEST['type']) && $_REQUEST['type'] == 1){
            $type = 1;
        }
        $this->assign('type', $type);
        return $this->fetch('course/listedit');
    }

}